package com.web.music.portal.mvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.mvc.SimpleFormController;

import com.web.music.interfaces.SecurityController;
import com.web.music.interfaces.UserService;
import com.web.music.model.User;

public class GrantRevokeController extends SimpleFormController {
	private SecurityController securityController;
	private UserService userService;

	public GrantRevokeController() {
		setCommandClass(User.class);
		setSessionForm(true);
	}
	
	@Override
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		User user = new User();
		if (!isFormSubmission(request)){	
			String idString = request.getParameter("id");
			if (idString != null && !idString.equals("")) {
				long id = Long.parseLong(request.getParameter("id"));
				return userService.getById(id);
			}			
		}
		return user;
	}

	@Override
	protected void doSubmitAction(Object object) throws Exception {
		User user = (User) object;
		if (user.getIsAdmin()==null || !user.getIsAdmin().equals("Y")){
			user.setIsAdmin("N");
		}
		userService.save(user);
	}
	

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public SecurityController getSecurityController() {
		return securityController;
	}

	public void setSecurityController(
			SecurityController securityController) {
		this.securityController = securityController;
	}



}
